package com.wx.jpa.controller;

import com.wx.jpa.entity.UserInfo;
import com.wx.jpa.entity2.UserInfo2;
import com.wx.jpa.service.UserService;
import com.wx.jpa.service2.UserService2;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;


/**
 * @ClassName TestController
 * @Author wx
 * @Description 用户测试
 * @Date 2018-12-26-21:10
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {


    @Resource
    private UserService userService;

    @Resource
    private UserService2 userService2;

    /**
     * @methodName: testMoreDataSource
     * @author: wx
     * @description: 测试多数据源操作
     * @param
     * @date: 2018/12/26
     * @return: void
     */
    @PostMapping(value = "/testMoreDataSource")
    public void  testMoreDataSource(){
        UserInfo userInfo = new UserInfo();
        userInfo.setName("db_1_wx");
        userInfo.setJobNumber("db_1_10086");
        userInfo.setCreateTime(new Date());
        userService.addUserInfo(userInfo);
        UserInfo2 userInfo2 = new UserInfo2();
        userInfo2.setName("db_2_wx");
        userInfo2.setJobNumber("db_2_10086");
        userInfo2.setCreateTime(new Date());
        userService2.addUserInfo(userInfo2);
    }

    @GetMapping(value = "/getUserList")
    public List<UserInfo> getUserList() {
        return userService.getUserList();
    }

    @GetMapping(value = "/getUserInfo")
    public UserInfo getUserInfoByName(@RequestParam("name") String name) {
        return userService.getUserByName(name);
    }

    @GetMapping(value = "/getCurrentUserList")
    public List<UserInfo> getCurrentUserList(){
        return userService.getCurrentUserList();
    }

    @GetMapping(value="/getPageUserList")
    public Page<UserInfo> getPageUserList(){
        return userService.getPageUserList();
    }

    @PostMapping(value = "/addUserInfo")
    public UserInfo addUserInfo(@RequestBody UserInfo userInfo) {
        return userService.addUserInfo(userInfo);
    }


    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));/*TimeZone时区，解决差8小时的问题*/
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
}
